package cn.fanyamin.web.struts;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.LazyValidatorForm;

import cn.fanyamin.business.RoleService;
import cn.fanyamin.domain.Module;

public class ModuleAction extends SecureBaseAction {
	private static Log logger = LogFactory.getLog(ModuleAction.class);

	public ActionForward list(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
		RoleService roleService = getWebApp().getRoleService();
	    List<Module> moduleList = roleService.getAllModuleList();
	    request.setAttribute("moduleList", moduleList);
		logger.info("moduleList: " + moduleList);
        return mapping.findForward("list");

	}
	public ActionForward create(ActionMapping mapping, ActionForm form,
	                            HttpServletRequest request, HttpServletResponse response) {
		RoleService roleService = getWebApp().getRoleService();
		Module module = getModuleFromForm(form);
		if(module == null) {
		    return mapping.findForward("failure");
		}
		int moduleID = roleService.createModule(module);
		module.setModuleID(moduleID);
		logger.info("create module: " + module);
		return mapping.findForward("success");
	}
	
    private Module getModuleFromForm(ActionForm form) {
		Module module = new Module();
		LazyValidatorForm moduleForm = (LazyValidatorForm) form;

		module.setModuleID(NumberUtils.toInt((String) moduleForm.get("moduleID")));
		module.setModuleName((String) moduleForm.get("moduleName"));		
		module.setDescription((String) moduleForm.get("description"));
		module.setModuleLink((String) moduleForm.get("moduleLink"));
		module.setParentModuleID(NumberUtils.toInt((String)moduleForm.get("parentModuleID")));

		return module;
	}

    public ActionForward retrieve(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        RoleService roleService = getWebApp().getRoleService();
        LazyValidatorForm moduleFrom = (LazyValidatorForm)form;
        int moduleID = NumberUtils.toInt((String)moduleFrom.get("moduleID"));
        Module module = roleService.retrieveModule(moduleID);
        request.setAttribute("module", module);
        return mapping.findForward("edit");
    }
    
    public ActionForward update(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        
        RoleService roleService = getWebApp().getRoleService();
        Module module = getModuleFromForm(form);
        roleService.updateModule(module);
        logger.info("update module, module=" + module);
        return mapping.findForward("success");
    }
    
    public ActionForward delete(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        RoleService RoleService = getWebApp().getRoleService();
        LazyValidatorForm ModuleFrom = (LazyValidatorForm)form;
        int ModuleID = NumberUtils.toInt((String)ModuleFrom.get("ModuleID"));
        RoleService.delete(ModuleID);
        logger.info("delete Module, ModuleID=" + ModuleID);
        return mapping.findForward("success");
    }
    
    public ActionForward add(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
    	logger.info("add Module");
    	return mapping.findForward("add");
    }
    
    public ActionForward edit(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        RoleService roleService = getWebApp().getRoleService();
        LazyValidatorForm ModuleFrom = (LazyValidatorForm)form;
        int moduleID = NumberUtils.toInt((String)ModuleFrom.get("moduleID"));
        Module Module = roleService.retrieveModule(moduleID);
        request.setAttribute("Module", Module);
        return mapping.findForward("edit");
    }
}
